package com.example.sofa_module.sofa.details;

import android.text.Editable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewbinding.ViewBinding;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.bw.emoji.Emoji;
import com.bw.emoji.EmojiUtil;
import com.bw.emoji.FaceFragment;
import com.example.common.mvvm.view.BaseActivity;
import com.example.imgload_sdk.glide.ImgLoad;
import com.example.sofa_module.R;
import com.example.sofa_module.databinding.ActivityDetailsBinding;
import com.example.sofa_module.sofa.details.entity.DetakisEntity;
import com.example.sofa_module.sofa.entity.BeanEntity;

import java.io.IOException;
import java.util.List;

import retrofit2.http.Path;
@Route(path = "/sofa/deatailsactivity")
public class DetailsActivity extends BaseActivity<ActivityDetailsBinding,DetailsViewModel>  implements FaceFragment.OnEmojiClickListener{

    @Autowired
    BeanEntity beanEntity;
    private DetailsAdapter detailsAdapter;
    private boolean isInput= false;

    @Override
    public View bindLayout() {
        return v.getRoot();
    }

    @Override
    public ViewBinding initBinding() {
        ActivityDetailsBinding activityDetailsBinding=ActivityDetailsBinding.inflate(inflater);
        return activityDetailsBinding;
    }

    @Override
    public void initView() {
        detailsAdapter = new DetailsAdapter();
    }

    @Override
    public void initData() {
        ARouter.getInstance().inject(this);
        vm.requestDealist(beanEntity.itemId);
        LogUtils.e(beanEntity.activityText+"");
        v.detalisIjk.setPlayUrl(beanEntity.url);
        v.detalisIjk.start();
        v.detalisIjk.onClick(v.detalisIjk);
        v.detalisIjk.setImgUrl(beanEntity.cover);
        v.itemVideoArtist.setText(beanEntity.author.name);
        v.itemVideoFeedsContent.setText(beanEntity.feeds_text);
        v.itemVideoType.setText(beanEntity.activityText);
        ImgLoad.loadMatchImg(beanEntity.author.avatar,v.itemVideoPic);

        vm.detail_list.observe(this, new Observer<List<DetakisEntity>>() {
            @Override
            public void onChanged(List<DetakisEntity> detakisEntities) {
                v.DetaiksRe.setLayoutManager(new LinearLayoutManager(DetailsActivity.this));
                v.DetaiksRe.setAdapter(detailsAdapter);
                detailsAdapter.setNewInstance(detakisEntities);
            }
        });

        v.detailEmoji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isInput){
                    isInput = true;
                    v.detailFrame.setVisibility(View.VISIBLE);
                    FaceFragment faceFragment = FaceFragment.Instance();
                    getSupportFragmentManager().beginTransaction().add(R.id.detail_frame,faceFragment).commit();
                }else{
                    isInput = false;
                    v.detailFrame.setVisibility(View.INVISIBLE);
                    v.detailFrame.removeAllViews();
                }
            }
        });
    }

    @Override
    public void onEmojiDelete() {
        String text = v.detailInput.getText().toString();
        if (text.isEmpty()) {
            return;
        }
        if ("]".equals(text.substring(text.length() - 1, text.length()))) {
            int index = text.lastIndexOf("[");
            if (index == -1) {
                int action = KeyEvent.ACTION_DOWN;
                int code = KeyEvent.KEYCODE_DEL;
                KeyEvent event = new KeyEvent(action, code);
                v.detailInput.onKeyDown(KeyEvent.KEYCODE_DEL, event);
                displayTextView();
                return;
            }
            v.detailInput.getText().delete(index, text.length());
            displayTextView();
            return;
        }
        int action = KeyEvent.ACTION_DOWN;
        int code = KeyEvent.KEYCODE_DEL;
        KeyEvent event = new KeyEvent(action, code);
        v.detailInput.onKeyDown(KeyEvent.KEYCODE_DEL, event);
        displayTextView();
    }

    private void displayTextView() {
        try {
            EmojiUtil.handlerEmojiText( v.detailInput,  v.detailInput.getText().toString(), this);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onEmojiClick(Emoji emoji) {
        if (emoji != null) {
            int index =  v.detailInput.getSelectionStart();
            Editable editable =  v.detailInput.getEditableText();
            if (index < 0) {
                editable.append(emoji.getContent());
            } else {
                editable.insert(index, emoji.getContent());
            }
        }
        displayTextView();
    }
}
